package Admin;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

public class EditAccount extends javax.swing.JDialog {

    ConnectRMI connectRMI = new ConnectRMI();
    private int                    id;
    private String                 checkedit;
    private javax.swing.JButton    btcancel;
    private javax.swing.JButton    btreset;
    private javax.swing.JButton    btsubmit;
    private javax.swing.JComboBox  cbbsex;
    private javax.swing.JLabel     lbaddress;
    private javax.swing.JLabel     lbbrithday;
    private javax.swing.JLabel     lbcmnd;
    private javax.swing.JLabel     lbcompanyname;
    private javax.swing.JLabel     lbemail;
    private javax.swing.JLabel     lbname;
    private javax.swing.JLabel     lbphone;
    private javax.swing.JLabel     lbsex;
    private javax.swing.JTextField ttbrithday;
    private javax.swing.JTextField ttcompanyname;
    private javax.swing.JTextField txtaddress;
    private javax.swing.JTextField txtcmnd;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtname;
    private javax.swing.JTextField txtphone;

    public String getCheckedit() {
        return checkedit;
    }

    public void setCheckedit(String checkedit) {
        this.checkedit = checkedit;
    }

    public EditAccount(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        txtemail = new javax.swing.JTextField();
        lbaddress = new javax.swing.JLabel();
        lbemail = new javax.swing.JLabel();
        ttcompanyname = new javax.swing.JTextField();
        txtaddress = new javax.swing.JTextField();
        lbcompanyname = new javax.swing.JLabel();
        lbphone = new javax.swing.JLabel();
        txtphone = new javax.swing.JTextField();
        btsubmit = new javax.swing.JButton();
        btreset = new javax.swing.JButton();
        btcancel = new javax.swing.JButton();
        lbsex = new javax.swing.JLabel();
        lbcmnd = new javax.swing.JLabel();
        lbbrithday = new javax.swing.JLabel();
        lbname = new javax.swing.JLabel();
        txtname = new javax.swing.JTextField();
        ttbrithday = new javax.swing.JTextField();
        txtcmnd = new javax.swing.JTextField();
        cbbsex = new javax.swing.JComboBox();

        setUndecorated(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setLocation(450, 150);

        lbsex.setText("Sex");

        lbcmnd.setText("Cmnd");

        lbbrithday.setText("Brithday");

        lbname.setText("Name");

        lbaddress.setText("Address");

        lbemail.setText("Email");

        lbcompanyname.setText("Companyname");

        lbphone.setText("Phone");

        btsubmit.setText("Submit");

        btreset.setText("Reset");
        btreset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ttcompanyname.setText("");
                txtaddress.setText("");
                txtemail.setText("");
                txtphone.setText("");
            }
        });

        btcancel.setText("Cancel");
        btcancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setCheckedit("close");
                setVisible(false);
            }
        });
        txtname.setText(Account.getA2());
        ttbrithday.setText(Account.getA4());

        txtcmnd.setText(Account.getA6());
        ttcompanyname.setText(Account.getA3());
        txtaddress.setText(Account.getA7());
        txtemail.setText(Account.getA9());
        txtphone.setText(Account.getA8());

        btsubmit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    EtowInterface obj = null;
                    Registry registry = LocateRegistry.getRegistry(connectRMI.host, connectRMI.port);
                    obj = (EtowInterface) registry.lookup(connectRMI.regis);
                    String name = txtname.getText();
                    String brithday = ttbrithday.getText();
                    String cmnd = txtcmnd.getText();
                    String sex = Integer.toString(cbbsex.getSelectedIndex());
                    String company = ttcompanyname.getText();
                    String address = txtaddress.getText();
                    String email = txtemail.getText();
                    int phone = Integer.parseInt(txtphone.getText());
                    id = Integer.parseInt(getCheckedit());
                    String result = obj.EditAccount(name, brithday, cmnd, sex, company, address, phone, email, id);
                    if (result.equals("ThanhCong")) {
                        JOptionPane.showMessageDialog(null, "Update Successful");
                        setCheckedit("close");
                        setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(null, "Update Fail");
                    }
                } catch (Exception ex) {

                }
            }
        });
        cbbsex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));
        if (Account.getA5().equals("Male") == true) {
            cbbsex.setSelectedItem("Male");
        } else {
            cbbsex.setSelectedItem("Female");
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lbcompanyname).addComponent(lbaddress)
                                                .addComponent(lbphone).addComponent(lbemail).addComponent(lbname)
                                                .addComponent(lbbrithday).addComponent(lbsex).addComponent(lbcmnd))
                                .addGap(18, 18, 18)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        txtcmnd,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        223, Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        ttbrithday,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        223, Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        txtname,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        223, Short.MAX_VALUE)
                                                                                .addGroup(
                                                                                        layout.createSequentialGroup()
                                                                                                .addComponent(btsubmit)
                                                                                                .addPreferredGap(
                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                .addComponent(btreset)
                                                                                                .addPreferredGap(
                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                .addComponent(btcancel))
                                                                                .addComponent(
                                                                                        txtemail,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        223, Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        txtphone,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        223, Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        txtaddress,
                                                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        223, Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        ttcompanyname,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        223, Short.MAX_VALUE))
                                                                .addGap(71, 71, 71))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(cbbsex,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addContainerGap()))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup()
                        .addContainerGap(31, Short.MAX_VALUE)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbname)
                                        .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbbrithday)
                                        .addComponent(ttbrithday, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbsex)
                                        .addComponent(cbbsex, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbcmnd)
                                        .addComponent(txtcmnd, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(lbcompanyname)
                                        .addComponent(ttcompanyname, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbaddress)
                                        .addComponent(txtaddress, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbphone)
                                        .addComponent(txtphone, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbemail)
                                        .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btsubmit).addComponent(btreset).addComponent(btcancel))
                        .addContainerGap()));

        pack();
    }

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditAccount.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditAccount.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditAccount.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditAccount.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                EditAccount dialog = new EditAccount(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

}
